//Cipher Class Body
////Graham Foster
////Content for cipher class for cipher solver

#include <iostream>
#include <string>
#include "cipher.h"
using namespace std;

int Cipher::newCipher()
{//TODO
	/*int i=0; //index control

	char string[200]

	cout << "Please enter string now, up to 200 characters: " << endl;
	
	while ((string[i]=getchar())!= '\r')
	{
		cout << string[i];// << endl;
		if (string[i]=='\r'){cout << "!!" << endl;}
		if (((string[i]>='A') && (string[i]<='Z')) || ((string[i]>='a') && (string[i]<='z')))
		{
         solution[i]='-';
		}
		else if ((caseSensitive==false) && (numbers==false))
		{
			solution[i]=string[i];
		}
		else
		{
			solution[i]='-';
		}
		i++;
		length=i;
		string[length]='\0';
		solution[length]='\0';
	}
	//getchar();*/
	return 0;
}

int Cipher::loadCipher(char fileName[])
{
	FILE *fin; //file pointer
	char tempchar=0; //temporary character to test for beginning of string

	//Quick check to make sure file is there
	if ((fin = fopen(fileName, "r"))==NULL)
	{
		return 1; //not finding the file is handled out in the parent function
	}

	char tempstr[20]; //for screening names of variables
	//this reads in all the data at the beginning of the file
	int boolHolder;
	fscanf(fin,"%s%d",tempstr,&boolHolder);
	caseSensitive = boolHolder;
	fscanf(fin,"%s%d",tempstr,&boolHolder);
	punctuation = boolHolder;
	fscanf(fin,"%s%d",tempstr,&boolHolder);
	numbers = boolHolder;
	
	//advance cursor to beginning of next string (the cipher text)
	while (tempchar<'!')
	{
		tempchar=getc(fin);
	}

	string[0]=tempchar;
	length=1;

	//read in characters so long as neither a newline nor the end of the file is encountered
	while ((tempchar!='\n') && (!feof(fin)))
	{
		fscanf(fin,"%c",&tempchar);
		if (tempchar!='\n')
		{
			string[length]=tempchar;
			length++;
		}
	}
	//close file
	fclose(fin);
	return 0;
}

int Cipher::saveCipher(char fileName[])
{

	FILE *fout; //file pointer
	//Quick check to make sure file is there
	if ((fout = fopen(fileName, "w"))==NULL)
	{
		return 1; //not finding the file is handled out in the parent function
	}

	//this writes all the data at the beginning of the file
	fprintf(fout,"%s %d\n","caseSensitive",caseSensitive);
	fprintf(fout,"%s %d\n","punctuation",punctuation);
	fprintf(fout,"%s %d\n","numbers",numbers);

	//write cipher with a newline at the end
	for (int i=0; i<length; i++)
	{
		fprintf(fout,"%c",string[i]);
	}
	fprintf(fout,"\n");
	//close the file
	fclose(fout);

	printf("save successful\n\n");
	return 0;
}

void Cipher::printCipher(Key k1)
{
	//move the screen down
	printf("\n\n\n\n\n\n\n\n\n\n\n\n\n****\n");
	
	//print the solution
	for (int i=0; ((i<length)&&(i<79)); i++) //to make sure it wraps with the solution fine
	{
		printf("%c", k1.lookupChar(string[i])); //prints translated character
	}
	cout << endl;
	
	//print the cipher text
	for (int i=0; ((i<length)&&(i<79)); i++) //to make sure it wraps with the solution fine
	{
		//cout << i << string[i]; handy diagnostic form
		cout << string[i];
	}
	
	//blank line
	printf("\n\n");
	
	//if necessary, wrap to a second set of lines
	if (length > 80)
	{
		//print the solution
		for (int i=79; i<(79 + length % 79); i++) //to make sure it wraps with the solution fine
		{
			printf("%c", k1.lookupChar(string[i]));
		}
		printf("\n");
		//print the cipher text
		for (int i=79; i<(79 + length % 79); i++) //to make sure it wraps with the solution fine
		{
			cout << string[i];
		}
	}
	//print out the length and the letter frequency
	cout << endl << "length: " << length << endl << endl;
	cout << endl << endl;
	printFreq(); //print out the relative frequencies of the letters in the cipher
}

void Cipher::calcFreq(int letterFreq[])
{
	//initialize empty array of 26 for letter frequencies
	//TODO This is currently limited to a 26 character alphabetic set
	for (int i=0; i<26; i++)
	{
		letterFreq[i]=0;
	}
	//believe it or not, chars can be accessed just as easily by number or character
	//cycle through the cipher and count letter frequencies
	//TODO This is not currently case-insensitive
	for (int i=0; i<length; i++)
	{
		letterFreq[string[i]-65]++;
	}
}

void Cipher::printFreq()
{//TODO maybe order the letters printed by frequency?
	int letterFreq[26];
	calcFreq(letterFreq);

	for (int i=65; i<91; i++)
	{
		printf(" %c ",i);
	}
	printf("\n");
	for (int i=0; i<26; i++)
	{
		printf ("%2d ", letterFreq[i]);
	}
	printf ("\n\n");
}
